<?php

/**
 * @file SendEmail
 * @author garypradoruiz <garypradoruiz@gmail.com>
 * @link mailto:garypradoruiz@gmail.com
 * @copyleft 2012 garypradoruiz
 * @license GPL
 * @version 1
 */
class Amz_Resource_SendEmailCopyUser {

    public function sendEmail($id_orden, $users, $email='',$type) {
        // list emails for send
        $toMails = $this->getEmailUsers($users);
        $orden = $this->getDataOrdenId($id_orden);
        $ordenJob = $this->getOrdenJob($id_orden);
        
        if($email != ''){
            $toMails[] = array('name' => $email, 'email' => $email);
        }

        if ($orden != null) {
            $smtpServer = 'mail.velcro-lab.com';
            $username = 'admpro@velcro-lab.com';
            $password = 'admpro';

            $config = array('ssl' => 'tls',
                'auth' => 'login',
                'username' => $username,
                'password' => $password);

            $transport = new Zend_Mail_Transport_Smtp($smtpServer, $config);

            // Set From & Reply-To address and name for all emails to send.
            Zend_Mail::setDefaultFrom('admpro@velcro-lab.com', 'Seguimiento Proyectos');
            // create view object
            $html = new Zend_View();
            $html->setScriptPath(APPLICATION_PATH . '/modules/orden/views/emails/');

            $html->assign('orden', $orden);
            $html->assign('ordenJob', $ordenJob);

            $request = Zend_Controller_Front::getInstance()->getRequest();
            //$url = $request->getScheme() . '://' . $request->getHttpHost() . '';
            $url = $request->getScheme() . '://' . $request->getHttpHost() . '/capataz';

            // Loop through messages
            foreach ($toMails as $to) {
                $html->assign('name', $to['name']);

                switch ($type) {
                    case 'DETALLE_ORDEN_USER_INTERNAL':
                        $html->assign('link', '');
                        $bodyText = $html->render('copy-detail.phtml');
                        $subject = 'Copia Detalle de Orden ' . ' / ' . $orden->code;
                        break;
                }

                // Email config send
                $mail = new Zend_Mail();
                $mail->addTo($to['email'], $to['name']);
                $mail->setSubject($subject);
                $mail->setBodyHtml($bodyText);
                $mail->send($transport);
            }

            // Reset defaults
            Zend_Mail::clearDefaultFrom();
        }
    }

    private function getEmailUsers($id_users) {
        $usersDB = new User_Model_DbTable_User();
        $toMails = array();

        foreach ($id_users as $val) {
            if ($val != null && $val != 0) {
                $rowUser = $usersDB->fetchRow('id=' . $val);
                $names = $rowUser->nombre . ' ' . $rowUser->apellidos;
                $toMails[] = array('name' => $names, 'email' => $rowUser->email);
            }
        }
        return $toMails;
    }

    /*
     * Get data orden Id only a row
     */

    private function getDataOrdenId($id_orden = 0) {
        $ordenDb = new Orden_Model_DbTable_Orden();

        $select = $ordenDb->select()->setIntegrityCheck(false)
                ->from(array('o' => 'orden'), array('id',
                    'code',
                    'title',
                    'description',
                    'create_date',
                    'end_date',
                    'status',
                    'deny_description')
                )->where('o.id=?', $id_orden)
                ->join(array('ou' => 'orden_user'), 'o.id = ou.id_orden')
                ->join(array('u' => 'user'), 'u.id = ou.id_user')
                ->join(array('cl' => 'client'), 'cl.id = u.client_departament', array('name'));

        $row = $ordenDb->fetchRow($select);
        if ($row != null)
            return $row;
        else
            return null;
    }

    public function getOrdenJob($id_orden) {
        $ordenJob = new Orden_Model_DbTable_OrdenJob();
        $result = $ordenJob->fetchAll('id_orden=' . $id_orden);
        if ($result != null)
            return $result;
        else
            return null;
    }
}
?>
